<template>
  <div>
    <!-- 导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 主体 -->
    <el-card>
      <!-- 搜索 & 添加按钮 -->
      <el-row>
        <el-col :span="13">
          <el-input
            v-model="getUserListParams.query"
            placeholder="请输入内容"
            class="input-with-select"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="search"
            ></el-button>
          </el-input>
        </el-col>

        <el-col :span="3" :offset="1">
          <el-button @click="dialogAddFormVisible = true"> 添加用户</el-button>
        </el-col>
      </el-row>

      <!-- 展示表格 -->
      <el-table :data="userList" border>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="姓名" prop="username"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="电话" prop="mobile"></el-table-column>
        <el-table-column label="角色" prop="role_name"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.mg_state"> </el-switch>

            {{ scope.$index }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              circle
            ></el-button>
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-setting"
              circle
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              circle
              @click="delFn(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getUserListParams.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="getUserListParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加用户弹出表单 -->
    <el-dialog title="添加用户" :visible.sync="dialogAddFormVisible">
      <el-form :model="addUserParams" :rules="addRules" ref="addform">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            v-model="addUserParams.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            v-model="addUserParams.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="addUserParams.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="电话" :label-width="formLabelWidth">
          <el-input
            v-model="addUserParams.mobile"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetAddForm('addform')">取 消</el-button>
        <el-button type="primary" @click="addUserFn('addform')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getusers, addusers, delusers } from "@/http/user.js";
export default {
  name: "P102110cUsers",

  data() {
    return {
      formLabelWidth: "100px",
      userList: [{}],
      total: 0, 
      getUserListParams: {
        query: "",
        pagenum: 1,
        pagesize: 2,
      },
      dialogAddFormVisible: false,
      addUserParams: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      addRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },

  mounted() {
    this.getUserList();
    console.log(this.lodash.max([1,2,3,4]));
  },

  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.getUserListParams.pagesize = val;
      this.getUserList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.getUserListParams.pagenum = val;
      this.getUserList();
    },
    async getUserList() {
      let res = await getusers(this.getUserListParams);
      console.log(res);
      this.userList = res.data.users; 
      this.total = res.data.total; 
    },
    addUserFn(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await addusers(this.addUserParams);
          if (res.meta.status == 201) {
            this.dialogAddFormVisible = false;
            this.getUserList();
          } else {
            alert("添加失败");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetAddForm(formName) {
      this.$refs[formName].resetFields();
    },
    async delFn(id) {
      let res = await delusers(id);
      this.getUserList();
    },
    search() {
      this.getUserListParams.pagenum = 1;
      this.getUserList();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-card,
.el-table,
.el-pagination {
  margin-top: 15px;
}
</style>